
using memo.Config;
using memo.Services;
using SqlSugar;
using StackExchange.Redis;

namespace memo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
            builder.Services.AddOpenApi();
            
            
            // 注册 SqlSugarScope
            builder.Services.AddSingleton<SqlSugarScope>(DbContext.GetInstance());
            // 注册 UserService
            builder.Services.AddSingleton<UserService>();
            // 注册 MemoService
            builder.Services.AddSingleton<MemoService>();
            // 注册 AutoMapper
            builder.Services.AddAutoMapper(typeof(MappingProfile));
            // 添加原生 Redis 客户端
            builder.Services.AddSingleton<IConnectionMultiplexer>(_ => {
                var configuration = ConfigurationOptions.Parse(builder.Configuration["Redis:Connection"]!);
                return ConnectionMultiplexer.Connect(configuration);
            });
            // 添加Redis分布式缓存服务
            builder.Services.AddStackExchangeRedisCache(options => {
                options.Configuration = builder.Configuration["Redis:Connection"];
                options.InstanceName = builder.Configuration["Redis:InstanceName"]; // 通过前缀实现数据隔离（当多个应用或服务共享同一个 Redis 服务器时）
            });
            

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.MapOpenApi();
            }

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
